var __assign =
  (this && this.__assign) ||
  function () {
    __assign =
      Object.assign ||
      function (t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
      };
    return __assign.apply(this, arguments);
  };
var __rest =
  (this && this.__rest) ||
  function (s, e) {
    var t = {};
    for (var p in s)
      if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === 'function')
      for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
          t[p[i]] = s[p[i]];
      }
    return t;
  };
import React, { useContext, useMemo } from 'react';
import { Button, Form, Input, Space, message } from 'antd';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { ConfigsContext } from '../..';
export function OptionsSet(props) {
  var handleChange = props.handleChange;
  var activeSchema = useContext(ConfigsContext).activeSchema;
  var form = Form.useForm()[0];
  var optionsStringified = useMemo(function () {
    var options = activeSchema.current.configs.inputProps.options;
    return options.map(function (item) {
      return {
        label: item.label,
        value: JSON.stringify(item.value)
      };
    });
  }, []);
  var handleBlur = function () {
    var allValues = form.getFieldsValue(true);
    var newOptions = allValues.options.filter(function (item) {
      return (
        (item === null || item === void 0 ? void 0 : item.label) !== undefined &&
        (item === null || item === void 0 ? void 0 : item.value) !== undefined
      );
    });
    try {
      var newOptionsParsed = newOptions.map(function (item) {
        return {
          label: item.label,
          value: JSON.parse(item.value)
        };
      });
      newOptionsParsed.length && handleChange(newOptionsParsed);
    } catch (e) {
      message.error('value值请正确输入JSON格式！');
    }
  };
  return React.createElement(
    React.Fragment,
    null,
    React.createElement(
      'div',
      null,
      React.createElement(
        'strong',
        null,
        '*\u6CE8\u610Fvalue\u503C\u9700\u8981\u8F93\u5165JSON\u683C\u5F0F\uFF01'
      )
    ),
    React.createElement(
      Form,
      { form: form },
      React.createElement(
        Form.List,
        { name: 'options', initialValue: optionsStringified },
        function (fields, _a) {
          var add = _a.add,
            remove = _a.remove;
          return React.createElement(
            React.Fragment,
            null,
            fields.map(function (_a) {
              var key = _a.key,
                name = _a.name,
                restField = __rest(_a, ['key', 'name']);
              return React.createElement(
                Space,
                { key: key, style: { display: 'flex' }, align: 'start' },
                React.createElement(
                  Form.Item,
                  __assign({}, restField, {
                    name: [name, 'label'],
                    rules: [{ required: true, message: '请定义label' }]
                  }),
                  React.createElement(Input, { placeholder: 'label', prefix: '"', suffix: '"' })
                ),
                React.createElement('span', null, ':'),
                React.createElement(
                  Form.Item,
                  __assign({}, restField, {
                    name: [name, 'value'],
                    rules: [{ required: true, message: '请定义value' }]
                  }),
                  React.createElement(Input, {
                    onBlur: handleBlur,
                    placeholder: 'value',
                    addonAfter: 'JSON'
                  })
                ),
                React.createElement(MinusCircleOutlined, {
                  onClick: function () {
                    remove(name);
                    handleBlur();
                  }
                })
              );
            }),
            React.createElement(
              Form.Item,
              null,
              React.createElement(
                Button,
                {
                  type: 'dashed',
                  onClick: function () {
                    return add();
                  },
                  block: true,
                  icon: React.createElement(PlusOutlined, null)
                },
                '\u65B0\u589E\u9009\u9879'
              )
            )
          );
        }
      )
    )
  );
}
